package com.danlu.user;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.danlu.mvc.Student;
import com.danlu.util.SqlSessionFactoryUtil;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "/user", description = "用户管理")
@Controller
public class UserController {
    //进入登录页面  
  @RequestMapping(value = "/login", method = RequestMethod.GET)
  public ModelAndView login() {
    return new ModelAndView("login", "command", new Student());
  }
  //判断登录请求  
  @ApiOperation(value = "登录", notes = "通过CAS单点登录验证身份信息")
  @ResponseBody
  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public Map<String, Object> login(String name,String password) {
    Map<String, Object> result = new HashMap<String, Object>();
    //判断用户名或者密码是否存在
    if(name == null || password == null || name.isEmpty() || password.isEmpty()  ) {
      result.put("status", 1);
      result.put("msg", "用户名或者密码不能为空");
      return result;
    }
    
    SqlSession session = SqlSessionFactoryUtil.openSession();
    UserMapper  userMapper = session.getMapper(UserMapper.class);
    String user1 = userMapper.getUserByNameAndPasswd(name,password);
    result.put("status", 0);
    result.put("data", user1);  
    return result;
  }

  
  @ApiOperation(value = "注册", notes = "注册新的用户")
  @ResponseBody
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public Map<String, Object> register( @RequestBody User user) {
    Map<String, Object> result = new HashMap<String, Object>();
    //判断用户名或者密码是否存在
    if(user.getName().isEmpty() || user.getPassword().isEmpty()  ) {
      result.put("status", 1);
      result.put("msg", "用户名或者密码不能为空");
      return result;
    }
    
    SqlSession session = SqlSessionFactoryUtil.openSession();
    UserMapper  userMapper = session.getMapper(UserMapper.class);
    int addResult =  userMapper.addUser(user);
    result.put("status", 0);
    result.put("data", addResult); 
    return result;
  }
  
  @ApiOperation(value = "注销用户", notes = "退出登录，删除用户session信息")
  @ApiImplicitParam(name = "17584557485", value = "33445", required = true, dataType = "Long")
  
  @RequestMapping(value = "/logout", method = RequestMethod.GET)
  public String logout(HttpSession session) {
    session.invalidate();
    // return new ModelAndView("login", "command", new Student());
    return "redirect:http://192.168.50.253:8090/cas/logout";
  }

}
